(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('filebeatConfigController', filebeatConfigController);
    filebeatConfigController.$inject = ['$scope','toaster', '$compile', 'searchService'];

    function filebeatConfigController($scope,toaster, $compile, searchService) {
        var notify = function (type, title, message) {
            toaster.pop(type, title, message);
        };

        $scope.condition = {};

        $scope._initPage = true;

        $scope.partitions = [
        ];

        $scope.ips = [
        ];

        searchService.getPartition({}).then(
            function (resp) {
                angular.forEach(resp.data, function (item) {
                    var partition = {};
                    partition.partitionEname = item.ename;
                    partition.partitionId = item.id;
                    $scope.partitions.push(partition);
                });
                searchService.getPartitionIp2Host({}).then(
                    function(resp){
                        angular.forEach(resp.data, function (item) {
                            var ip = {};
                            angular.forEach($scope.partitions, function (partitionItem) {
                                if (item.partitionId === partitionItem.partitionId) {
                                    ip.partitionEname = partitionItem.partitionEname;
                                    ip.ip = item.ip;
                                    $scope.ips.push(ip);
                                }
                            });
                        });
                        console.log($scope.ips);
                    },function(err){
                    }
                );
            }, function (err) {
            }
        );



        $scope.editable = {
            mode: "popup",
            template: kendo.template($("#gridEditor").html())
        };

        $scope.toolbar = [
            "create",
            {
                template: '<kendo-button ng-click="update($event)"><span class="k-icon k-i-edit"></span>编辑</kendo-button>'
            },
            {
                template: '<kendo-button ng-click="destroy($event)"><span class="k-icon k-i-delete"></span>删除</kendo-button>'
            }

        ];
        $scope.pageable = {
            refresh: true,
            pageSizes: true,
            numeric: false,
            input: true
        };

        $scope.Model = kendo.data.Model.define({
            id: "id",
            fields: {
                projectName: {type: "string"},
                ip: {type: "string"},
                port: {type: "number"},
                username: {type: "string"},
                password: {type: "string"},
                partitionEname: {type: "string"},
                middleware: {type: "string"},
                serverDB: {type: "string"},
                xcomVersion: {type: "string"},
                redisHost: {type: "string"}
            }
        });


        $scope.middlewares = [
            {middleware: "tux", middlewareText: "Tuxedo"},
            {middleware: "ixt", middlewareText: "iXTop"}
        ];

        $scope.serverDBs = [
            {serverDB: "ora", serverDBText: "Oracle"},
            {serverDB: "db2", serverDBText: "db2"}
        ];



        $scope.columns = [
            {
                field: "id", title: "id", hidden: true
            },
            {
                field: "projectName", title: "项目名", width: 100, hidden: true
            },
            {
                field: "partitionEname", title: "分区", width: 100
            },
            {
                field: "ip", title: "IP", width: 100
            },
            {
                field: "port", title: "端口", width: 100
            },
            {
                field: "username", title: "用户名", width: 120
            },
            {
                field: "password", title: "密码", width: 120
            },
            {
                field: "middleware", title: "中间件类型", width: 120
            },
            {
                field: "serverDB", title: "数据库类型", width: 120
            },
            {
                field: "xcomVersion", title: "xcom版本", width: 120
            },
            {
                field: "redisHost", title: "redis主机地址", width: 130
            },
            {
                template: '<kendo-button ng-click="upload($event)" class="k-primary">上传</kendo-button>'
            }
        ];

        $scope.dataSource = new kendo.data.DataSource({
            autoBind: false,
            transport: {
                read: function (e) {
                    searchService.queryFilebeatConfig().then(
                        function(resp){
                            e.success(resp.data);
                        },function(err){
                            notify('error', '失败', '查询失败'+err);
                            e.error(err);
                        }
                    )
                },
                create: function (e) {
                    searchService.saveFilebeatConfig(e.data.models[0]).then(function (resp) {
                        e.success(resp.data);
                        notify('success', '成功', '新增成功！');
                    }, function (error) {
                        e.error(error);
                        notify('error', '失败', error);

                    });
                },
                update: function (e) {
                    searchService.saveFilebeatConfig(e.data.models[0]).then(function (resp) {
                        notify('success', '成功', '修改成功！');
                        e.success(resp.data);
                    }, function (error) {
                        e.error(error);
                        notify('error', '失败', error);
                    });
                },
                destroy: function (e) {
                    searchService.deleteFilebeatConfig(e.data.models[0]).then(function (resp) {
                        notify('success', '成功', '删除成功！');
                        e.success();
                    }, function (error) {
                        e.error(error);
                        notify('error', '失败', error);
                    });
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            requestStart: function (e) {
                if ($scope._initPage) {
                    e.preventDefault();
                }
            },
            batch: true,
            pageSize: 15,
            schema: {
                model: $scope.Model
            }
        });

        $scope.query = function () {
            $scope._initPage = false;
            $scope.grid.dataSource.read();
        };

        $scope.update = function (e) {
            var selected = $scope.grid.select();
            if (selected.length == 0) {
                //alert('No record selected')
                notify('waring', '提示', '请先选择一行！');
            } else {
                $scope.grid.editRow(selected);
            }
        };
        $scope.destroy = function (e) {
            var selected = $scope.grid.select();
            if (selected.length == 0) {
                //alert('No record selected')
                notify('waring', '提示', '请先选择一行！');
            } else {
                $scope.grid.removeRow(selected);
            }
        };

        $scope.upload = function (e) {
            var dataItem = $scope.grid.dataItem($(e.currentTarget).closest("tr"));
           searchService.uploadFilebeatConfig(dataItem.toJSON()).then(function (response) {
               notify('success', '成功', '上传成功！');
           }, function (error) {
               notify('error', '失败', error);
           })
        };
    }
})();
